package com.mercury.article.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mercury.article.entity.HomeArticle;
import com.mercury.article.entity.HomeArticleDetail;
import com.mercury.article.entity.vo.ArticleInfoVo;
import com.mercury.article.service.HomeArticleDetailService;
import com.mercury.article.service.HomeArticleService;
import com.mercury.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zwn
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
@Api(value = "文章模块控制器")
public class HomeArticleController {

    @Autowired
    private HomeArticleService articleService;

    @Autowired
    private HomeArticleDetailService detailService;

    @ApiOperation(value = "显示首页文章列表")
    @GetMapping("/articleList")
    public R showArticleList(){
        ArrayList<HomeArticle> homeArticleList = articleService.showArticleList();
        if (homeArticleList == null){
            return R.error();
        }else {
            return R.ok().data("articleList",homeArticleList);
        }
    }

    @ApiOperation(value = "根据类别显示文章")
    @GetMapping("/ariticleByType/{type}")
    public R getarticleBytype(@PathVariable int type) {
        List<HomeArticle> articleList = articleService.getArticleByType(type);
        return R.ok().data("articleList",articleList);
    }


    /**
     * @date 2021/11/3 11:47
     * @params current
     * @params  limit
     * @return
     * @description 分页展示文章列表
    */
    @ApiOperation(value = "分页展示")
    @GetMapping("articlePage/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {

        //创建page对象
        Page<HomeArticle> pageArticle = new Page<>(current, limit);
        articleService.page(pageArticle,null);
        long total = pageArticle.getTotal();
        List<HomeArticle> records = pageArticle.getRecords();
        return R.ok().data("items",records).data("total",total);
    }

    /**
     * @date 2021/11/1 15:26
     * @params id
     * @return
     * @description 通过文章 id 获取文章详情，返回文章详情数据
    */
    @ApiOperation(value = "获取文章详情和文章类别")
    @GetMapping("getArticle/{id}")
    public R getTeacher(@PathVariable String id){
        ArticleInfoVo articleInfoVo = articleService.getArticleById(id);
        return R.ok().data("articleInfoVo", articleInfoVo);
    }

    /**
     * @date 2021/11/7 15:35
     * @params id
     * @return
     * @description 逻辑删除文章，将文章表和文章详情表的 deleted 属性由 0 改为 1
    */
    @ApiOperation("逻辑删除文章")
    @DeleteMapping("delete/{id}")
    public R deleteArticle(@PathVariable String id){
        boolean articleRes = articleService.removeById(id);
        boolean detailRes = detailService.removeById(id);
        if(!articleRes){
            return R.error().message("删除文章列表失败");
        }
        if(!detailRes){
            return R.error().message("删除文章详情失败");
        }
        return R.ok();
    }

    /**
     * @date 2021/11/7 15:36
     * @params articleInfoVo
     * @return
     * @description 通过传入的数据解析成 article 和 articleDetail 两个类，并将其存入到数据库
    */
    @ApiOperation("插入文章类别和详情表")
    @PostMapping("addArticle")
    public R addArticle(@RequestBody ArticleInfoVo articleInfoVo){
        String id = articleService.addArticle(articleInfoVo);
        return R.ok().data("articleId" , id);
    }


    @ApiOperation("更新文章")
    @PostMapping("updateArticle")
    public R updateArticle(@RequestBody ArticleInfoVo articleInfoVo){
        int res = articleService.updateArticle(articleInfoVo);
        return R.ok().message("更新成功");
    }

}

